package com.moneyhelper.oss.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.Callback;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.PutObjectResult;
import com.moneyhelper.oss.config.OssConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author yanggld
 * @Date 2019/7/8-17:03
 * @Description
 * 文件上传
 */
@Slf4j
@Component
public class FileUploadUtil {

	@Autowired
	private OssConfig ossConfig;

	/**
	 * 通过数据流 上传图片
	 * @param inputStream
	 * @throws FileNotFoundException
	 */
	public String uploadImageByStream(InputStream inputStream,String fileName) throws FileNotFoundException {
		String endpoint = ossConfig.getEndpoint();
		String accessKeyId = ossConfig.getAccessKeyId();
		String accessKeySecret = ossConfig.getAccessKeySecret();

		// 创建OSSClient实例。
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

		// 上传文件流。
		PutObjectResult putObjectResult = ossClient.putObject(ossConfig.getBucketName(),fileName , inputStream);

		// 设置URL过期时间为30年。
		Date now = new Date();
		DateTime offset = DateUtil.offset(now, DateField.YEAR, 30);
		Date expiration = new Date(offset.getTime());
		// 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
		URL url = ossClient.generatePresignedUrl(ossConfig.getBucketName(), fileName, expiration);
		log.info("上转文件之后的url:{}"+url);

		// 关闭OSSClient。
		ossClient.shutdown();

		return url.toString();
	}

	/**
	 * 通过文件 上传图片
	 * @param file
	 * @throws FileNotFoundException
	 */
	public String uploadImageByFile(File file,String fileName) throws FileNotFoundException {
		FileInputStream inputStream = new FileInputStream(file);
		return uploadImageByStream(inputStream,fileName);
	}

	/**
	 * 通过文件地址 上传图片
	 * @param filePath
	 * @throws FileNotFoundException
	 */
	public String uploadImageByPath(String filePath,String fileName) throws FileNotFoundException {
		File file = new File(filePath);
		return uploadImageByFile(file,fileName);
	}

	/**
	 * 通过文件地址删除文件
	 * @param url 文件地址
	 * @return
	 */
	public String deleteFileByUrl(String url){
		return deleteFileByName(FileUtil.getFileNameByUrl(url));
	}

	/**
	 * 通过文件名称删除文件
	 * @param fileName 文件名称
	 * @return
	 */
	public String deleteFileByName(String fileName){
		String endpoint = ossConfig.getEndpoint();
		String accessKeyId = ossConfig.getAccessKeyId();
		String accessKeySecret = ossConfig.getAccessKeySecret();
		String bucketName = ossConfig.getBucketName();

		// 创建OSSClient实例。
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// 删除文件。
		ossClient.deleteObject(bucketName, fileName);
		// 关闭OSSClient。
		ossClient.shutdown();

		return "SUCCESS";
	}

	/**
	 * 批量删除文件
	 * @param fileNameList 文件名称集合
	 * @return
	 */
	public String deleteFileList(List<String> fileNameList){
		String endpoint = ossConfig.getEndpoint();
		String accessKeyId = ossConfig.getAccessKeyId();
		String accessKeySecret = ossConfig.getAccessKeySecret();
		String bucketName = ossConfig.getBucketName();

		// 创建OSSClient实例。
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

		DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(fileNameList));
		List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
		log.info("批量删除的文件名称:{}"+JSONObject.toJSONString(deletedObjects));

		// 关闭OSSClient。
		ossClient.shutdown();

		return "删除文件";
	}
}
